﻿using System;
using System.Collections.Generic;
using System.Data;
//using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using PessModel;
using PessMvc.Lib;

namespace PessMvc.Controllers
{
    public class ArchiveDisplayNamesController : Controller
    {
        private PESSEntities db = new PESSEntities();

        // GET: ArchiveDisplayNames
        public ActionResult Index(int activityID)
        {
            IArchiveBL ibl = new ArchiveBL(this.ModelState);
            ibl.AddDisplayNames(activityID);

            var archiveDisplayNames = db.ArchiveDisplayNames.Where(n=>n.ActivityID==activityID);

            //Retrieve activity name.
            string ActivityName = "";
            var activity = db.Activities.Where(a => a.ID == activityID).FirstOrDefault();
            if (activity != null)
            {
                ActivityName = activity.Name;
            }
            ViewBag.ActivityName = ActivityName;

            return View(archiveDisplayNames.ToList());
        }

        // GET: ArchiveDisplayNames/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            ArchiveDisplayName archiveDisplayName = db.ArchiveDisplayNames.Find(id);
            if (archiveDisplayName == null)
            {
                return HttpNotFound();
            }
            return View(archiveDisplayName);
        }

        // GET: ArchiveDisplayNames/Create
        public ActionResult Create()
        {
            ViewBag.ActivityID = new SelectList(db.Activities, "ID", "Name");
            return View();
        }

        // POST: ArchiveDisplayNames/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "ActivityID,PropertyName,DisplayName,Visibility")] ArchiveDisplayName archiveDisplayName)
        {
            if (ModelState.IsValid)
            {
                db.ArchiveDisplayNames.Add(archiveDisplayName);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.ActivityID = new SelectList(db.Activities, "ID", "Name", archiveDisplayName.ActivityID);
            return View(archiveDisplayName);
        }

        // GET: ArchiveDisplayNames/Edit/5
        public ActionResult Edit(int? activityID,string propertyName)
        {
            if (activityID == null||propertyName==null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            ArchiveDisplayName archiveDisplayName = db.ArchiveDisplayNames.Find(activityID,propertyName);
            if (archiveDisplayName == null)
            {
                return HttpNotFound();
            }
            ViewBag.ActivityID = new SelectList(db.Activities, "ID", "Name", archiveDisplayName.ActivityID);
            return View(archiveDisplayName);
        }

        // POST: ArchiveDisplayNames/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "ActivityID,PropertyName,DisplayName,Visibility")] ArchiveDisplayName archiveDisplayName)
        {
            if (ModelState.IsValid)
            {
                db.Entry(archiveDisplayName).State = System.Data.Entity.EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index",new { activityID=archiveDisplayName.ActivityID});
            }
            //ViewBag.ActivityID = new SelectList(db.Activities, "ID", "Name", archiveDisplayName.ActivityID);
            return View(archiveDisplayName);
        }

        // GET: ArchiveDisplayNames/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            ArchiveDisplayName archiveDisplayName = db.ArchiveDisplayNames.Find(id);
            if (archiveDisplayName == null)
            {
                return HttpNotFound();
            }
            return View(archiveDisplayName);
        }

        // POST: ArchiveDisplayNames/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            ArchiveDisplayName archiveDisplayName = db.ArchiveDisplayNames.Find(id);
            db.ArchiveDisplayNames.Remove(archiveDisplayName);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
